package product_logic

import (
	"context"
	"strings"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/repository/models"
	"wx-uniapp-go/third_party/oss"

	"github.com/samber/lo"
)

type ProductLogic struct {
	Ctx context.Context
}

func NewProductLogic(ctx context.Context) *ProductLogic {
	return &ProductLogic{Ctx: ctx}
}

// ListProduct 查询商品列表
func (l *ProductLogic) ListProduct(req *dto.ListProductReq) (*dto.ListProductResp, error) {

	products, count, err := models.ProductFindByPage(l.Ctx, req.Keyword, req.CategoryId, req.TagId, req.DeliveryType, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}
	// 转换响应
	list := make([]*dto.ProductItem, len(products))
	for i, product := range products {
		// 转换规格信息
		specInfo := make([]dto.SpecInfo, len(product.SpecInfo))
		for j, spec := range product.SpecInfo {
			specInfo[j] = dto.SpecInfo{
				SpecName:  spec.SpecName,
				SpecId:    spec.SpecId,
				SpecPrice: 0, // 这里需要根据业务逻辑设置具体价格
			}
		}

		list[i] = &dto.ProductItem{
			Id:          product.Id,
			Title:       product.Title,
			Cover:       oss.Cli.JoinPath(product.Cover),
			Price:       product.Price,
			OriginPrice: product.OriginPrice,
			IsRecommend: product.IsRecommend,
			IsNew:       product.IsNew,
			IsHot:       product.IsHot,
			MinBuyNum:   product.MinBuyNum,
			MaxBuyNum:   product.MaxBuyNum,
			SpecInfo:    specInfo,
			SalesVolume: product.SalesVolume,
			Stock:       product.Stock,
			SkuList:     product.SkuList,
		}
	}

	return &dto.ListProductResp{
		Total:    count,
		List:     list,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, nil
}

// ListProductModel 查询商品列表(模块专用)
func (l *ProductLogic) ListProductModel(req *dto.ListProductReq) (*dto.ListProductModelPageResp, error) {

	tags, err := l.ListModel()
	if err != nil {
		return nil, err
	}
	tagIds := lo.Map(tags.List, func(v *dto.ProductModel, _ int) string {
		return v.Id
	})
	products, count, err := models.ProductFindByTagsPage(l.Ctx, tagIds, req.DeliveryType, req.Page, 2)
	if err != nil {
		return nil, err
	}
	// 转换响应
	list := make(map[string][]*dto.ProductItem, len(tags.List))

	for _, v := range tags.List {
		list[v.Id] = make([]*dto.ProductItem, 0)
		for _, product := range products {
			// 转换规格信息
			if !strings.Contains(strings.Join(product.Tags, ","), v.Id) {
				continue
			}
			specInfo := make([]dto.SpecInfo, len(product.SpecInfo))
			for j, spec := range product.SpecInfo {
				specInfo[j] = dto.SpecInfo{
					SpecName:  spec.SpecName,
					SpecId:    spec.SpecId,
					SpecPrice: 0, // 这里需要根据业务逻辑设置具体价格
				}
			}

			list[v.Id] = append(list[v.Id], &dto.ProductItem{
				Id:          product.Id,
				Title:       product.Title,
				Cover:       oss.Cli.JoinPath(product.Cover),
				Price:       product.Price,
				OriginPrice: product.OriginPrice,
				IsRecommend: product.IsRecommend,
				IsNew:       product.IsNew,
				IsHot:       product.IsHot,
				MinBuyNum:   product.MinBuyNum,
				MaxBuyNum:   product.MaxBuyNum,
				SpecInfo:    specInfo,
				SalesVolume: product.SalesVolume,
				Stock:       product.Stock,
				SkuList:     product.SkuList,
			})
		}
	}

	return &dto.ListProductModelPageResp{
		Total:     count,
		List:      list,
		ModelList: tags.List,
		Page:      req.Page,
		PageSize:  req.PageSize,
	}, nil
}
